package com.ktm.gecco.crawler.boche;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.htmlparser.util.ParserException;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Service;

import com.geccocrawler.gecco.GeccoEngine;
import com.geccocrawler.gecco.request.HttpGetRequest;
import com.geccocrawler.gecco.spring.SpringPipelineFactory;
import com.ktm.gecco.common.utils.SpringContextUtil;
import com.ktm.gecco.crawler.boche.entity.BocheCar;
import com.ktm.gecco.crawler.boche.eventListener.BocheCarEventListener;
import com.ktm.gecco.crawler.boche.service.impl.BocheCarServiceImpl;
import com.ktm.gecco.crawler.main.util.LoginWebBrower;
import com.ktm.gecco.entity.ScheduleJob;

/**
 */
@Service
public class BocheCarTask /*extends BaseUserLog */implements Job {

    @SuppressWarnings("static-access")
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        ScheduleJob scheduleJob = (ScheduleJob) context.getMergedJobDataMap().get("scheduleJob");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        System.out.println("任务名称 = [" + scheduleJob.getName() + "]" + " 在 " + dateFormat.format(new Date()) + " 时运行");
        SpringPipelineFactory springPipelineFactory = SpringContextUtil.getBean("springPipelineFactory");

        //保存日志
//        System.out.println("博车网车辆数据采集开始！");
//        UserLogMapper logmapper = ctx.getBean(UserLogMapper.class);
//        logmapper.saveUserLog(new UserLog("博车网任务采集开始！","博车网"));

        BocheCarServiceImpl.count=0;
        BocheCar.taskName  = scheduleJob.getName();
        BocheCar.taskGroup = scheduleJob.getGroup();

        //保存任务到已结束任务
//        EndJobMapper jobmapper = ctx.getBean(EndJobMapper.class);
//        jobmapper.addEndJob(new EndScheduleJob(scheduleJob.getName(),scheduleJob.getGroup()));

        //获取cookies
        String requestCookie = "";
        String responseCookie = "";
        LoginWebBrower loginWebBrower = new LoginWebBrower();
        try {
            requestCookie=loginWebBrower.firstlogin();
            responseCookie=loginWebBrower.login(requestCookie);
        } catch (IOException | ParserException e) {
            System.out.println("获取cookie失败");
            e.printStackTrace();
        }

        HttpGetRequest start = new HttpGetRequest("http://bochewang.com.cn/PaimaiHui/?t=636072071696085134");
        start.setCharset("GBK");
        start.addCookie(".ASPXAUTH", responseCookie);
        start.addCookie("ASP.NET_SessionId", requestCookie);
//        start.addHeader("Request Method", "GET");
        start.addHeader("Host", "bochewang.com.cn");
        start.addHeader("Upgrade-Insecure-Requests", "1");
        start.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        start.addHeader("Accept-Encoding", "gzip, deflate, sdch");
        start.addHeader("Cache-Control", "max-age=0");
        GeccoEngine.create()
        .pipelineFactory(springPipelineFactory)
        .classpath("com.ktm.gecco.crawler.boche")
        //开始抓取的页面地址
        .start(start)
        .loop(false)
        //单个爬虫每次抓取完一个请求后的间隔时间
        .interval(2000)
        .setEventListener(new BocheCarEventListener())
        //非阻塞方式运行
        .run();
    }
}